package com.yztstudy.Listener;

import com.yztstudy.Service.ProductService;
import com.yztstudy.to.OrderToProduct;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品的消息队列监听
 */

@Component
public class ProductRabbitMQListener {

    @Autowired
    private ProductService productService;


    /**
     * TODO 监听扣减库存，增加销量
     * @param orderToProducts 商品主键+数量的集合
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "sub.queue"), exchange = @Exchange(value = "topic.ex"), key = "sub.number"))
    public void subNumber(List<OrderToProduct> orderToProducts)
    {
        // TODO 1.直接扣减库存
        productService.subNumber(orderToProducts);
    }
}
